<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

#获取页面变量
$action = trim($_GET['action']);
$id= trim($_REQUEST['id']) + 0;
	$quote['id'] = string_sql($id);
#如果有group_id
if ($id > 0) {
	$sql = "SELECT * FROM members_info WHERE id=$id";
	$_members_info = $database->queryFirst($sql);
	if (!$_members_info['id']) {
		$_REQUEST['referrer'] = "index.php";
		redirect("记录不存在!");
	}
}
#得到post的值
$username = trim($_REQUEST['username']);
	$quote['username'] = string_sql($username);
$password = trim($_REQUEST['password']);
$name = trim($_REQUEST['name']);
	$quote['name'] = string_sql($name);
$sex = trim($_REQUEST['sex']);
	$quote['sex'] = string_sql($sex);
$birthday = trim($_REQUEST['birthday']);
	$quote['birthday'] = string_sql($birthday);
$nation = trim($_REQUEST['nation']);
	$quote['nation'] = string_sql($nation);
$company = trim($_REQUEST['company']);
	$quote['company'] = string_sql($company);
$job = trim($_REQUEST['job']);
	$quote['job'] = string_sql($job);
$phone = trim($_REQUEST['phone']);
	$quote['phone'] = string_sql($phone);
$tel = trim($_REQUEST['tel']);
	$quote['tel'] = string_sql($tel);
$email = trim($_REQUEST['email']);
	$quote['email'] = string_sql($email);
$qq = trim($_REQUEST['qq']);
	$quote['qq'] = string_sql($qq);
$address = trim($_REQUEST['address']);
	$quote['address'] = string_sql($address);
$zipcode = trim($_REQUEST['zipcode']);
	$quote['zipcode'] = string_sql($zipcode);
$region = trim($_REQUEST['region']);
	$quote['region'] = string_sql($region);
$memo = trim($_REQUEST['memo']);
	$quote['memo'] = string_sql($memo);

#如果是编辑
if ($action == "edit.form" || $action == "view.form") {
        $uid = $_members_info['id'];
	$username = $_members_info['username'];
	$name = $_members_info['name'];
        $sex = $_members_info['sex'];
	$birthday = $_members_info['birthday'];
	$nation = $_members_info['nation'];
	$company = $_members_info['company'];
	$job = $_members_info['job'];
	$phone = $_members_info['phone'];
	$tel = $_members_info['tel'];
	$email = $_members_info['email'];
	$qq = $_members_info['qq'];
	$address = $_members_info['address'];
	$zipcode= $_members_info['zipcode'];
	$region = $_members_info['region'];
        $memo =  $_members_info['memo'];
}
#如果是添加操作
if ($action == "add.do") {
        verify_field("username", "用户名", "empty");
        verify_field("name", "姓名", "empty");
	verify_field("password", "密码", "empty");
	verify_field("tel", "手机", "empty");
	verify_field("region", "网点", "empty");
        verify_field("email", "电子邮箱", "email");

	if (!count($error_strs)) {
		$sql = "INSERT INTO members_info ".
               "(username, password, name,sex,birthday,nation,company,job".
                ",phone,tel,email,qq,address,zipcode".
                ",region,memo".
                " ) VALUES (".$quote['username'].", ".string_sql(md5($password)).", ".$quote['name'].", ".
                        $quote['sex'].", ".$quote['birthday'].", ".$quote['nation'].", ".$quote['company'].", ".
                        $quote['job'].", ".$quote['phone'].", ".$quote['tel'].", ".
                        $quote['email'].", ".$quote['qq'].", ".$quote['address'].", ".
                        $quote['zipcode'].", ".$quote['region'].", ".$quote['memo'].");";
		$database->execute($sql);		
		$_REQUEST['referrer'] = "index.php";
		redirect("添加会员成功!");
	} else {
		$action = "add.form";
	}
#如果是编辑操作
} else if ($action == "edit.do" && $id > 0) {
    
        verify_field("name", "姓名", "empty");
	verify_field("tel", "手机", "empty");
	verify_field("region", "网点", "empty");
        verify_field("email", "电子邮箱", "email");
        
	if (!count($error_strs)) {
		$sql = "UPDATE members_info SET name = ".$quote['name'].iif(strlen($password)>0, ",password=".string_sql(md5($password))).
                ", sex = ".$quote['sex'].", birthday = ".$quote['birthday'].", nation = ".$quote['nation'].", company = ".$quote['company'].
                ", job = ".$quote['job'].", phone = ".$quote['phone'].", tel = ".$quote['tel'].", email = ".$quote['email'].
                ", qq = ".$quote['qq'].", address = ".$quote['address'].", zipcode = ".$quote['zipcode'].", region = ".$quote['region'].", memo = ".$quote['memo'].
                " WHERE id = ".$quote['id'].";";
                $rs = $database->execute($sql);
		$_REQUEST['referrer'] = "index.php";
		redirect("修改会员成功!");
	} else {
		$action = "edit.form";
	}
#如果是删除操作
} else if ($action == "delete.do" && $id > 0) {
	#删除会员信息
	$database->execute("DELETE FROM members_info WHERE id = ".$quote['id'].";");
        #删除files文件
        $rf = $database->queryArray("SELECT * FROM members_file WHERE uid=".$quote['id']);
        if(count($rs)>0){
            foreach($rs as $r){
                $database->execute("DELETE FROM members_file WHERE id = ".$r['id'].";");
                @unlink(_CONST_UPLOAD_PATH.$r['file']);
            }
        }
        #删除留言
        $rf = $database->queryArray("SELECT * FROM members_message WHERE uid=".$quote['id']);
        if(count($rs)>0){
            foreach($rs as $r){
                $database->execute("DELETE FROM members_message WHERE pid = ".$r['id'].";");
                $database->execute("DELETE FROM members_message WHERE id = ".$r['id'].";");
            }
        }
        #删除卡绑定
        $rf = $database->queryArray("SELECT * FROM members_card WHERE uid=".$quote['id']);
        if(count($rs)>0){
            foreach($rs as $r){
                $database->execute("UPDATE members_card SET status = 0 WHERE pid = ".$r['id'].";");
                $database->execute("DELETE FROM members_card WHERE id = ".$r['id'].";");
            }
        }
        
        $_REQUEST['referrer'] = "index.php";
        redirect("删除会员成功!");
}
#样式
$css = array();
$js = array();
#菜单
$menu = "members";
include_once("../layout/_header.php");
?>
        <div class="main">
       		<div class="traininc-nav-list">
				<ul>
                                        <?php
if($action == "add.form"){
?>
<li><a href="index.php">会员列表</a></li>
<li class="selected"><a href="javascript:;">新增会员</a></li>
<?php
}elseif($action == "edit.form"){
?>
<li><a href="index.php">会员列表</a></li>
<li class="selected"><a href="javascript:;">修改会员</a></li>
<?php
}else{
?>
<li class="selected"><a href="javascript:;">基本信息</a></li>
<li><a href="<?= _CONST_BACKEND_URL ?>/files/index.php?uid=<?=$uid?>">文件上传</a></li>
<li><a href="<?= _CONST_BACKEND_URL ?>/message/index.php?uid=<?=$uid?>">留言管理</a></li>
<li><a href="<?= _CONST_BACKEND_URL ?>/cardbind/index.php?uid=<?=$uid?>">绑定会员卡</a></li>
<?php
}
?>
				</ul>
			</div>
<?
if ($action == "add.form") {
?>
<form action="edit.php?action=add.do" method="post" id="form" name="form">
			<div class="box-con">
				<table class="table-input">
                                        <tr>
						<td class="title">用户名</td>
						<td><input type="text" size="30" name="username" value="<?= htmlspecialchars($username) ?>" > <?= show_field_error('username') ?></td>
						<td class="title">密码</td>
						<td><input type="password" name="password" size="30" value=""><?= show_field_error('password') ?></td>
					</tr>
					<tr>
						<td class="title">姓名</td>
						<td><input type="text" size="30" name="name" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('name') ?></td>
						<td class="title">性别</td>
						<td>
                                                    <select name="sex">
                                                        <option value="男">男</option>
                                                        <option value="女">女</option>
                                                        <option value="无">无</option>
                                                    </select>
                                                </td>
					</tr>
					<tr>
						<td class="title">出生年月</td>
						<td><input type="text" size="30" name="birthday" value="<?= htmlspecialchars($birthday) ?>" ></td>
						<td class="title">民族</td>
						<td><input type="text" size="30" name="nation" value="<?= htmlspecialchars($nation) ?>" ></td>
					</tr>
					<tr>
						<td class="title">单位</td>
						<td><input type="text" size="30" name="company" value="<?= htmlspecialchars($company) ?>" ></td>
						<td class="title">职务</td>
						<td><input type="text" size="30" name="job" value="<?= htmlspecialchars($job) ?>" ></td>
					</tr>
					<tr>
						<td class="title">座机</td>
						<td><input type="text" size="30" name="phone" value="<?= htmlspecialchars($phone) ?>" ></td>
						<td class="title">手机</td>
						<td><input type="text" size="30" name="tel" value="<?= htmlspecialchars($tel) ?>" ></td>
					</tr>
					<tr>
						<td class="title">电子邮箱</td>
						<td><input type="text" size="30" name="email" value="<?= htmlspecialchars($email) ?>" > <?= show_field_error('email') ?></td>
						<td class="title">qq</td>
						<td><input type="text" size="30" name="qq" value="<?= htmlspecialchars($qq) ?>" ></td>
					</tr>
					<tr>
						<td class="title">家庭地址</td>
						<td><input type="text" size="30" name="address" value="<?= htmlspecialchars($address) ?>" ></td>
						<td class="title">邮编</td>
						<td><input type="text" size="30" name="zipcode" value="<?= htmlspecialchars($zipcode) ?>" ></td>
					</tr>
                                        <tr>
						<td class="title">网点</td>
						<td>
                                                    <select name="region">
                                                    <?php
                                                    #获得网点
                                                    $rgs = $database->queryArray("SELECT * FROM region");
                                                    for($i=0;$i<count($rgs);$i++){
                                                        $rg = $rgs[$i];
                                                        echo "<option value='".$rg['name']."'>".$rg['name']."</option>";
                                                    }
                                                    ?>
                                                    </select>
                                                </td>
						<td class="title">备注</td>
						<td>
                                                    <textarea cols="28" rows="2" name="memo" ><?= htmlspecialchars($memo) ?></textarea>
                                                </td>
					</tr>
                                        
					<tr>
						<td class="title"> </td>
						<td><input type="submit" name="" value="提交" class="btnA" /><input type="reset" name="" value="取消" class="btnB" /></td>
					</tr>
				</table>
			</div>
</form>
<?
} else if ($action == "edit.form") {
?>
<form action="edit.php?action=edit.do" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
			<div class="box-con">
				<table class="table-input">
                                        <tr>
						<td class="title">用户名</td>
						<td><input type="text" size="30" name="username" value="<?= htmlspecialchars($username) ?>" disabled="true"></td>
						<td class="title">密码</td>
						<td><input type="password" name="password" size="30" value=""><?= show_field_error('password') ?></td>
					</tr>
					<tr>
						<td class="title">姓名</td>
						<td><input type="text" size="30" name="name" value="<?= htmlspecialchars($name) ?>" > <?= show_field_error('name') ?></td>
						<td class="title">性别</td>
						<td>
                                                    <select name="sex">
                                                        <option value="男" <?=iif($rg['sex']=='男', "selected='selected'")?>>男</option>
                                                        <option value="女" <?=iif($rg['sex']=='女', "selected='selected'")?>>女</option>
                                                        <option value="无" <?=iif($rg['sex']=='无', "selected='selected'")?>>无</option>
                                                    </select>
                                                </td>
					</tr>
					<tr>
						<td class="title">出生年月</td>
						<td><input type="text" size="30" name="birthday" value="<?= htmlspecialchars($birthday) ?>" ></td>
						<td class="title">民族</td>
						<td><input type="text" size="30" name="nation" value="<?= htmlspecialchars($nation) ?>" ></td>
					</tr>
					<tr>
						<td class="title">单位</td>
						<td><input type="text" size="30" name="company" value="<?= htmlspecialchars($company) ?>" ></td>
						<td class="title">职务</td>
						<td><input type="text" size="30" name="job" value="<?= htmlspecialchars($job) ?>" ></td>
					</tr>
					<tr>
						<td class="title">座机</td>
						<td><input type="text" size="30" name="phone" value="<?= htmlspecialchars($phone) ?>" ></td>
						<td class="title">手机</td>
						<td><input type="text" size="30" name="tel" value="<?= htmlspecialchars($tel) ?>" ></td>
					</tr>
					<tr>
						<td class="title">电子邮箱</td>
						<td><input type="text" size="30" name="email" value="<?= htmlspecialchars($email) ?>" > <?= show_field_error('email') ?></td>
						<td class="title">qq</td>
						<td><input type="text" size="30" name="qq" value="<?= htmlspecialchars($qq) ?>" ></td>
					</tr>
					<tr>
						<td class="title">家庭地址</td>
						<td><input type="text" size="30" name="address" value="<?= htmlspecialchars($address) ?>" ></td>
						<td class="title">邮编</td>
						<td><input type="text" size="30" name="zipcode" value="<?= htmlspecialchars($zipcode) ?>" ></td>
					</tr>
                                        <tr>
						<td class="title">网点</td>
						<td>
                                                    <select name="region">
                                                    <?php
                                                    #获得网点
                                                    $rgs = $database->queryArray("SELECT * FROM region");
                                                    for($i=0;$i<count($rgs);$i++){
                                                        $rg = $rgs[$i];
                                                        echo "<option value='".$rg['name']."' ".iif($rg['name']==$region, "selected='selected'")." >".$rg['name']."</option>";
                                                    }
                                                    ?>
                                                    </select>
                                                </td>
                                                <td class="title">备注</td>
						<td>
                                                    <textarea cols="28" rows="2" name="memo" ><?= htmlspecialchars($memo) ?></textarea>
                                                </td>
					</tr>

					<tr>
						<td class="title"> </td>
						<td><input type="submit" name="" value="修改" class="btnA" /></td>
					</tr>
				</table>
			</div>
</form>
<?php
}else{
?>
<form action="edit.php?action=edit.form" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
			<div class="box-con">
				<table class="table-view">
                                    	<tr>
						<td class="title">用户名</td>
						<td><?= htmlspecialchars($username) ?></td>
						<td class="title">网点</td>
						<td><?= htmlspecialchars($region) ?></td>
					</tr>
					<tr>
						<td class="title">姓名</td>
						<td><?= htmlspecialchars($name) ?></td>
						<td class="title">性别</td>
						<td><?= htmlspecialchars($sex) ?></td>
					</tr>
					<tr>
						<td class="title">出生年月</td>
						<td><?= htmlspecialchars($birthday) ?></td>
						<td class="title">民族</td>
						<td><?= htmlspecialchars($nation) ?></td>
					</tr>
					<tr>
						<td class="title">单位</td>
						<td><?= htmlspecialchars($company) ?></td>
						<td class="title">职务</td>
						<td><?= htmlspecialchars($job) ?></td>
					</tr>
					<tr>
						<td class="title">座机</td>
						<td><?= htmlspecialchars($phone) ?></td>
						<td class="title">手机</td>
						<td>＋<?= htmlspecialchars($tel) ?></td>
					</tr>
					<tr>
						<td class="title">电子邮箱</td>
						<td><?= htmlspecialchars($email) ?></td>
						<td class="title">qq</td>
						<td><?= htmlspecialchars($qq) ?></td>
					</tr>
					<tr>
						<td class="title">家庭地址</td>
						<td><?= htmlspecialchars($address) ?></td>
						<td class="title">邮编</td>
						<td><?= htmlspecialchars($zipcode) ?></td>
					</tr>
					<tr>
						<td class="title"> </td>
						<td><input type="submit" name="" value="修改" class="btnA" /></td>
                                                <td class="title">备注</td>
						<td><?= htmlspecialchars($memo) ?></td>
					</tr>
				</table>
			</div>
    </form>
<?php
}
?>
        </div>
<?
include_once("../layout/_footer.php");
?>